<?php

/**
 * Framework_Object
 *
 * @author Application Support Group
 * @copyright University of Georgia
 * @package BAMF
 * @filesource
*/

require_once 'Log.php';

/**
 * Framework_Object
 *
 * The base object class for most of the classes that we use in our framework.
 * Provides basic logging and set/get functionality.
 *
 * @author Application Support Group
 * @package BAMF
*/
abstract class Framework_Object {
    /**
     * $log
     *
     * @var mixed $log Instance of PEAR Log 
    */
	protected $log;

    /**
     * $me
     *
     * @var mixed $me Instance of ReflectionClass
    */
	protected $me;


    /**
     * __construct
     * 
     * @author Application Support Group
     * @access public 
    */
	public function __construct() {
		#$this->log = Log::factory('file',FRAMEWORK_LOG_FILE);
		$this->log = Log::singleton('file',FRAMEWORK_LOG_FILE);
		$this->me = new ReflectionClass($this);
	}

    /**
     * setFrom
     *
     * @author Application Support Group
     * @access public
     * @param mixed $data Array of variables to assign to instance
     * @return void
    */
	public function setFrom($data) {
		if (is_array($data) && count($data)) {
			$valid = get_class_vars(get_class($this));

			foreach ($valid as $var => $val) {

				if (isset($data[$var])) {
					$this->$var = $data[$var];
				}
			}
		}
	}

    /**
     * toArray
     *
     * @author Application Support Group
     * @access public
     * @return mixed Array of member variables keyed by variable name
    */
	public function toArray() {
		$defaults = $this->me->getDefaultProperties();
		$return = array();
		foreach ($defaults as $var => $val) {
			if ($this->$var instanceof Framework_Object) {
				$return[$var] = $this->$var->toArray();
			} else {
				$return[$var] = $this->$var;
			}
		}

		return $return;
	}


    /**
     * logMessage
     *
     * @author Application Support Group
     * @access public
     * @param string $aMessage Message string to log
     * @return void
    */
	protected function logMessage( $aMessage ) {
		$this->log->log( $aMessage ) ;
	}



    /**
     * getClass
     *
     * @author Application Support Group
     * @access public
     * @return string
    */
	final public function getClass() {
		if( @is_object( $this )) {
			return get_class($this) ;
		} else {
			return __CLASS__ ;
		}
	}



    /**
     * __destruct
     *
     * @author Application Support Group
     * @access public
     * @return void
    */
	public function __destruct() {
		if ($this->log instanceof Log) {
			$this->log->close();
		}
	}


}

?>
